﻿namespace ImageManager.MyType.FieldType
{
    public class MyBoolean : IDynamicSet, IMyPropColumnType
    {
        static MyBoolean _True = new MyBoolean(true);
        static MyBoolean _False = new MyBoolean(false);
        public MyBoolean()
        {
            Val = false;
        }
        public MyBoolean(object val)
        {
            InitWithVal(val);
        }
        public static MyBoolean True { get { return _True; } }
        public static MyBoolean False { get { return _False; } }
        private MyBoolean(bool val)
        {
            Val = val;
        }
        public IDynamicSet InitWithVal(object val)
        {
            Val = val?.ToString()?.ToUpper() == "Y";
            return this;
        }
        public bool Val { get; set; }
        public override string ToString()
        {
            return Val ? "Y" : "N";
        }


        public static implicit operator MyBoolean(bool val)
        {
            if (val)
                return _True;
            else
                return _False;
        }
        public static implicit operator MyBoolean(string val)
        {
            if (val != null && val.ToUpper() == "Y") return _True;
            return _False;
        }
        public static bool operator ==(MyBoolean a, string b)
        {
            return a.ToString() == b;
        }
        public static bool operator !=(MyBoolean a, string b)
        {
            return a.ToString() != b;
        }
        public override int GetHashCode()
        {
            return this.Val.GetHashCode();
        }
        public override bool Equals(object? obj)
        {
            if (obj == null) return false;
            MyBoolean o = (MyBoolean)obj;
            return o.Val.Equals(this.Val);
        }

        public object CreateWithVal(object val)
        {
            return new MyBoolean(val);
        }

        public object GetVal()
        {
            return this.ToString();
        }
    }
}
